package com.alks.function.controller.biocontrol.deptwork;

import cn.hutool.core.date.DateTime;
import com.alibaba.excel.util.MapUtils;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.deserializer.LocalDateTimeDeserialize;
import com.alks.function.data.request.biocontrol.deptwork.SchHeadMovRequest;
import com.alks.function.data.request.biocontrol.deptwork.ScheduleLotRequest;
import com.alks.function.data.vo.biocontrol.deptwork.ScheduleHeadExcelVo;
import com.alks.function.service.PcProdDynamicService;
import com.alks.function.service.biocontrol.deptwork.IPsScehduleService;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = {"生控部-生控作业-成型预排作业(建档)", "生控部-生控作业-成型预排作业(安排)", "生控部-生控作业-成型预排作业(查询)"})
@RequiredArgsConstructor
@RoleCheck(prgIds = "140103")
public class PsScehduleController {
    private final IPsScehduleService service;
    private final PcProdDynamicService pcProdDynamicService;

    /**
     * 建档查询部分
     *
     * @return
     */

    @GetMapping("/getLineQuery")
    @ApiOperation(value = "基础资料建档-成型资料定义-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getLineQuery() {
        return service.getLineQuery();
    }

    @PutMapping("/getHolidayQuery")
    @ApiOperation(value = "基础资料建档-节假日定义-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getHolidayQuery(@RequestBody PageParam request) {
        return service.getHolidayQuery(request);
    }

    @PutMapping("/getIEQuery")
    @ApiOperation(value = "基础资料建档-型体IEt建档-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getIEQuery(@RequestBody PageParam request,
                                   @RequestParam(required = false) String style) {
        return service.getIEQuery(request, style);
    }

    @PutMapping("/getCustomQuery")
    @ApiOperation(value = "基础资料建档-成型产品定义-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getCustomQuery(@RequestBody PageParam request) {
        return service.getCustomQuery(request);
    }

    @GetMapping("/getCurveQuery")
    @ApiOperation(value = "基础资料建档-学习曲线-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getCurveQuery() {
        return service.getCurveQuery();
    }

    @GetMapping("/getLtCommonQuery")
    @ApiOperation(value = "基础资料建档-时程门槛定义-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getLtCommonQuery() {
        return service.getLtCommonQuery();
    }

    @PutMapping("/getDayPercentQuery")
    @ApiOperation(value = "基础资料建档-生产线日排定义-查询", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo getDayPercentQuery(@RequestBody PageParam request,
                                           @RequestParam(required = false) @ApiParam("生产线名称") String line,
                                           @RequestParam(required = false) @ApiParam("日期")
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime time) {
        return service.getDayPercentQuery(request, line, time);
    }

    /**
     * 建档新增部分
     *
     * @return
     */

    @PutMapping("/lineAdd")
    @ApiOperation(value = "基础资料建档-成型资料定义新增-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo lineAdd(@RequestBody PsLine request) {
        return service.lineAdd(request);
    }

    @PutMapping("/holidayAdd")
    @ApiOperation(value = "基础资料建档-节假日定义新增-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo holidayAdd(@RequestBody PsHoliday request) {
        return service.holidayAdd(request);
    }

    @PutMapping("/iEAdd")
    @ApiOperation(value = "基础资料建档-型体IE建档新增-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo iEAdd(@RequestBody PsStyleIe request) {
        return service.iEAdd(request);
    }

    @PutMapping("/customAdd")
    @ApiOperation(value = "基础资料建档-成型产品定义新增-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo customAdd(@RequestBody PsLineCustom request) {
        return service.customAdd(request);
    }

    @PutMapping("/curveAdd")
    @ApiOperation(value = "基础资料建档-学习曲线新增-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo curveAdd(@RequestBody PsStyleCurve request) {
        return service.curveAdd(request);
    }


    @PutMapping("/dayPercentAdd")
    @ApiOperation(value = "基础资料建档-生产线日排定义-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo dayPercentAdd(@RequestBody PsDayPercent request) {
        return service.dayPercentAdd(request);
    }

    /**
     * 建档修改部分
     */
    @PutMapping("/lineUpdate")
    @ApiOperation(value = "基础资料建档-成型资料定义更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo lineUpdate(@RequestBody PsLine request,
                                   @RequestParam @ApiParam("旧车间代码") String workshopOld) {
        return service.lineUpdate(request, workshopOld);
    }

    @PutMapping("/holidayUpdate")
    @ApiOperation(value = "基础资料建档-节假日定义更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo holidayUpdate(@RequestBody PsHoliday request,
                                      @RequestParam @ApiParam("旧日期")
                                      @JsonDeserialize(using = LocalDateTimeDeserialize.class)
                                      LocalDateTime holidayDateOld) {
        return service.holidayUpdate(request, holidayDateOld);
    }

    @PutMapping("/iEUpdate")
    @ApiOperation(value = "基础资料建档-型体IE建档更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo iEUpdate(@RequestBody PsStyleIe request,
                                 @RequestParam @ApiParam("旧型体") String styleOld) {
        return service.iEUpdate(request, styleOld);
    }

    @PutMapping("/customUpdate")
    @ApiOperation(value = "基础资料建档-成型产品定义更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo customUpdate(@RequestBody PsLineCustom request) {
        return service.customUpdate(request);
    }

    @PutMapping("/curveUpdate")
    @ApiOperation(value = "基础资料建档-学习曲线更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo curveUpdate(@RequestBody PsStyleCurve request) {
        return service.curveUpdate(request);
    }

    @PutMapping("/ltCommonUpdate")
    @ApiOperation(value = "基础资料建档-时程门槛定义更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo ltCommonUpdate(@RequestBody PsLtCommon request) {
        return service.ltCommonUpdate(request);
    }

    @PutMapping("/dayPercentUpdate")
    @ApiOperation(value = "基础资料建档-生产线日排定义更新-更新", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo dayPercentUpdate(@RequestBody PsDayPercent request) {
        return service.dayPercentUpdate(request);
    }

    /**
     * 建档删除部分
     */
    @PostMapping("/lineDelete")
    @ApiOperation(value = "基础资料建档-成型资料定义删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo lineDelete(@RequestParam("workshopOld") String workshopOld) {
        return service.lineDelete(workshopOld);
    }

    @PostMapping("/holidayDelete")
    @ApiOperation(value = "基础资料建档-节假日定义删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo holidayDelete(@RequestParam("holidayDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime holidayDate) {
        return service.holidayDelete(holidayDate);
    }

    @PostMapping("/iEDelete")
    @ApiOperation(value = "基础资料建档-型体IE建档删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo iEDelete(@RequestParam("style") String style) {
        return service.iEDelete(style);
    }

    @PostMapping("/customDelete")
    @ApiOperation(value = "基础资料建档-成型产品定义删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo customDelete(@RequestParam Integer id) {
        return service.customDelete(id);
    }

    @PostMapping("/curveDelete")
    @ApiOperation(value = "基础资料建档-学习曲线删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo curveDelete(@RequestParam Integer id) {
        return service.curveDelete(id);
    }

    @PostMapping("/dayPercentDelete")
    @ApiOperation(value = "基础资料建档-生产线日排定义删除-删除", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo dayPercentDelete(@RequestParam Integer id) {
        return service.dayPercentDelete(id);
    }


    /**
     * 建档特殊功能部分
     */
    @PostMapping("/monthHolidayAdd")
    @ApiOperation(value = "基础资料建档-插入下个月周日日期-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo monthHolidayAdd() {
        return service.monthHolidayAdd();
    }

    @PostMapping("/monthPercentAdd")
    @ApiOperation(value = "基础资料建档-插入一个月日排资料-新增", tags = "生控部-生控作业-成型预排作业(建档)")
    public ResponseInfo monthPercentAdd(@RequestParam @ApiParam("日期") String date) {
        return service.monthPercentAdd(date);
    }

    /**
     * 进度安排部分
     */

    @GetMapping("/scheduleLotUp")
    @ApiOperation(value = "进度安排-更新指令-新增", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleLotUp() {
        return service.scheduleLotUp();
    }

    @PutMapping("/getScheduleLotQuery")
    @ApiOperation(value = "进度安排-订单资料-可条件查询", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo getScheduleLotQuery(@RequestBody ScheduleLotRequest request) {
        return service.getScheduleLotQuery(request);
    }

    @PostMapping("/scheduleLotTF")
    @ApiOperation(value = "进度安排-指令结束/恢复-可条件查询", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleLotTF(@RequestParam @ApiParam("指令") String lotNo,
                                      @RequestParam @ApiParam("指令结束(Y 指令结束  N 指令恢复)") String schFlag) {
        return service.scheduleLotTF(lotNo, schFlag);
    }

    @PostMapping("/updateScheduleHead")
    @ApiOperation(value = "进度安排-取进度安排-数据更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo updateScheduleHead() {
        return service.updateScheduleHead();
    }

    @PostMapping("/getScheduleHeadQuery")
    @ApiOperation(value = "进度安排-进度安排资料-查询", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo getScheduleHeadQuery(@RequestParam @ApiParam("生产线") String line,
                                             @RequestParam(required = false) @ApiParam("指令") String lotNo,
                                             @RequestParam(required = false) @ApiParam("型体") String style) {
        return service.getScheduleHeadQuery(line, lotNo, style);
    }

    @PutMapping("/indexUpDown")
    @ApiOperation(value = "进度安排-上移下移-批量更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo indexUpDown(@RequestBody SchHeadMovRequest request) {
        return service.indexUpDown(request);
    }

    @PutMapping("/indexMove")
    @ApiOperation(value = "进度安排-批量改序号-批量更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo indexMove(@RequestBody SchHeadMovRequest request) {
        return service.indexMove(request);
    }

    @PutMapping("/lineMove")
    @ApiOperation(value = "进度安排-批量移线-批量更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo lineMove(@RequestBody SchHeadMovRequest request) {
        return service.lineMove(request);
    }

    @PostMapping("/scheduleHeadUp")
    @ApiOperation(value = "进度安排-修改IE日产能-更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleHeadUp(@RequestParam @ApiParam("IE日产能") String stdQty,
                                       @RequestParam @ApiParam("指令") String lotNo) {
        return service.scheduleHeadUp(lotNo, stdQty);
    }

    @PostMapping("/cptScheduleHead")
    @ApiOperation(value = "进度安排-计算进度表-更新", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo cptScheduleHead(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time) {
        return service.cptScheduleHead(time);
    }

    @PostMapping("/scheduleHeadAdd")
    @ApiOperation(value = "进度安排-安排进度-新增", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleHeadAdd(@RequestBody List<String> lotNos,
                                        @RequestParam String line) {
        return service.scheduleHeadAdd(lotNos, line);
    }

    @PutMapping("/scheduleHeadDelete")
    @ApiOperation(value = "进度安排-取消安排-删除", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleHeadDelete(@RequestBody List<String> lotNos) {
        return service.scheduleHeadDelete(lotNos);
    }

    @WebLogger
    @PostMapping("/scheduleHeadExpord")
    @ApiOperation(value = "进度安排-导出进度表-导出EXCEL", tags = "生控部-生控作业-成型预排作业(安排)")
    public ResponseInfo scheduleHeadExpord(HttpServletResponse response) {
        List<ScheduleHeadExcelVo> vos = service.scheduleHeadExpord();
        /*设置模版名和EXCEL名*/
        String templetName = "成型进度模版";
        String excelName = "成型进度表" + DateUtils.dateTimeNow();
        Map<String,String> dateMap= MapUtils.newHashMap();
        dateMap.put("date", DateFormatUtils.format(DateTime.now(),"yyyy/MM/dd"));
        /*写入EXCEL*/
        try {
              ExcelUtils.sheet(2).creatByTemplate(response, templetName, excelName ,vos ,dateMap);
        } catch (IOException e) {
            return ResponseInfo.error("导出失败");
        }
        return null;
    }

    /**
     * 进度查询部分
     */

    @PutMapping("/getScheduleQuery")
    @ApiOperation(value = "进度查询-分页可条件查询", tags = "生控部-生控作业-成型预排作业(查询)")
    public ResponseInfo getScheduleQuery(@RequestBody ScheduleLotRequest request) {
        return service.getScheduleQuery(request);
    }

    @GetMapping("/getStyleBack")
    @ApiOperation(value = "型体查询-回显", tags = "生控部-生控作业-成型预排作业(查询)")
    public ResponseInfo getStyleBack() {
        return service.getStyleBack();
    }
}
